import {Component, OnInit} from '@angular/core';
import {Project} from "../../project/entity/Project";
import {ProjectFindService} from "../../project/service/project.find.service";
import {Params, ActivatedRoute, Router} from "@angular/router";
import 'rxjs/add/operator/switchMap';
import {Page} from "../../shared/entity/Page";
import {DomSanitizer} from "@angular/platform-browser";
import {ProjectCategory} from "../entity/ProjectCategory";
@Component({
  selector: 'category',
  templateUrl: './category.html',
  styleUrls:['./category.css']
})
export class CategoryComponent implements OnInit{
  public page:Page<Project> = new Page<Project>();
  public  pageHtml:any;
  public categoryList:Array<ProjectCategory> = new Array<ProjectCategory>();
  private categoryId:string;

  ngOnInit(): void{
    this.categoryList = JSON.parse(sessionStorage.getItem("categoryList"));
    this.activatedRouter.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.projectService.getProjectPageByCategory(this.categoryId=params["id"],params["pageNo"]))
      .subscribe((page: Page<Project>) =>this.initResult(page));

  }


  private initResult(page:Page<Project>){
    this.page = page;

    // this.pageHtml = this.sanitizer.bypassSecurityTrustScript(this.page.projectPage);
  }
  public setPage(pageNo:string){
    this.router.navigate(['/category',{id:this.categoryId+"", pageNo: pageNo}]);
    //   this.projectService.getProjectPageByCategory(num)
    // .then((page: Page<Project>) =>this.initResult(page));
  }
  constructor(
    private activatedRouter: ActivatedRoute,
    private projectService: ProjectFindService,
    private router:Router,
    private sanitizer: DomSanitizer) {

  }
  public getProjectPageByCategory(categoryId:string){

  }
  public toProjectIndex(id:string){

    this.router.navigate(['/project/index/',id]);
  }

}
